<?php

namespace App\Http\Controllers\Api;

use App\Common\Libs\ArrayHelper;
use App\Consts\ExceptionCode;
use App\Exceptions\Error;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class SiteController extends BaseController
{
    public $validate = [
        "phoneLogin" => ["rules" => [
            "phone" => "required|string|max:11|phone",
            "code" => "required|string|max:6",
        ]],
        "pwdLogin" => ["rules" => [
            "phone" => "required|string|max:11|phone",
            "password" => "required|string|max:1000",
        ]],
    ];

    /**
     * @path /site/phone-login
     * @category 登录
     * @title 手机号验证码登录
     * @method POST
     * @params [{"name":"phone","type":"text","required":1,"example":"","desc":"手机号"},{"name":"code","type":"text","required":1,"example":"","desc":"验证码"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"用户ID","phone":"手机号","nickname":"用户昵称","avatar":"用户头像，如1.jpg","status":"用户状态，0异常，1正常","special_signature":"个性签名","birthday":"生日时间戳","gender":"性别，1男，2女，3保密","description":"个人简介","province":"省ID","city":"市ID","industry":["1","2"],"job":[],"education":"学历ID","is_new":false,"created_at":"创建时间戳","access_token":"令牌","identity":1,"login_method":"登录方式，1手机号验证码，2手机号密码，3QQ，4微信，5微博","server_time":"服务器时间"},"message":"OK"}
     */
    public function phoneLogin(Request $request)
    {
        $params = $request->all();

        $data = service()->SiteService->phoneLogin(ArrayHelper::merge($this->getBaseInfo($params), [
            'token' => $this->token,
        ]));

        return $this->apiReturn($data);
    }

    /**
     * @path /site/pwd-login
     * @category 登录
     * @title 手机号密码登录
     * @method POST
     * @params [{"name":"phone","type":"text","required":1,"example":"","desc":"手机号"},{"name":"password","type":"text","required":1,"example":"","desc":"密码"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"用户ID","phone":"手机号","nickname":"用户昵称","avatar":"用户头像，如1.jpg","status":"用户状态，0异常，1正常","special_signature":"个性签名","birthday":"生日时间戳","gender":"性别，1男，2女，3保密","description":"个人简介","province":"省ID","city":"市ID","industry":["1","2"],"job":[],"education":"学历ID","is_new":false,"created_at":"创建时间戳","access_token":"令牌","identity":1,"login_method":"登录方式，1手机号验证码，2手机号密码，3QQ，4微信，5微博","server_time":"服务器时间"},"message":"OK"}
     */
    public function pwdLogin(Request $request)
    {
        $params = $request->all();

        $data = service()->SiteService->pwdLogin(ArrayHelper::merge($this->getBaseInfo($params), [
            'token' => $this->token,
        ]));

        return $this->apiReturn($data);
    }

    /**
     * @path /site/bind-wx
     * @category 登录
     * @title 微信登录
     * @method POST
     * @params [{"name":"phone","type":"text","required":1,"example":"","desc":"手机号"},{"name":"code","type":"text","required":1,"example":"","desc":"验证码"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"用户ID","phone":"手机号","nickname":"用户昵称","avatar":"用户头像，如1.jpg","status":"用户状态，0异常，1正常","special_signature":"个性签名","birthday":"生日时间戳","gender":"性别，1男，2女，3保密","description":"个人简介","province":"省ID","city":"市ID","industry":["1","2"],"job":[],"education":"学历ID","is_new":false,"created_at":"创建时间戳","access_token":"令牌","identity":1,"login_method":"登录方式，1手机号验证码，2手机号密码，3QQ，4微信，5微博","server_time":"服务器时间"},"message":"OK"}
     */
    public function bindWx(Request $request)
    {
        if (!$this->token) {
            return $this->apiReturn(Error::handle(lang('Third oauth already expired'), ExceptionCode::LOGIN_FAIL));
        }

        $params = $request->all();
        $params['token'] = $this->token;

        $data = service()->SiteService->bindPhoneAfterThirdOAuth($params);

        return $this->apiReturn($data);
    }

    /**
     * where：区分环境：d开发，t测试，没有此参数/p为正式
     * user_id有这个参数的时候为绑定
     * $stateParams = [
     *    'url' => '要跳转的URL',
     *    'where' => 'd',
     *    'tourist_id' => '24为游客ID'
     *
     *    'user_id' => '24位用户ID，只有绑定是才有此参数',
     * ];
     */
    public function wxCallback(Request $request)
    {
        $params = $request->all();

        $data = service()->SiteService->wxCallback($params['code'], $params['state']);

        return $this->apiReturn($data);
    }
}
